﻿using SqlSugar;
using System;
using System.Linq;

namespace LD
{
    public class TB_Screw : BaseTable
    {
        /// <summary>
        /// 电批数据中的No
        /// </summary>
        public int No { get; set; }
        public string Name { get; set; }
        /// <summary>
        /// 父系ID
        /// </summary>
        public string Pid { get; set; }
        /// <summary>
        /// 单个螺丝条码
        /// </summary>
        public string Barcode { get; set; }
        /// <summary>
        /// 螺丝结果
        /// </summary>
        public string Result { get; set; }
        public string CCD_Result { get; set; }
        /// <summary>
        /// 扭矩结果
        /// </summary>
        public string Torque_Result
        {
            get
            {
                if (Actual_Torque >= TorqueMin
                    && Actual_Torque <= TorqueMax
                    )
                {
                    return "OK";
                }
                else
                {
                    this.Remark = "扭矩超出规格上下限";
                    LogHelper.Instance.Warr($"【位置:{location}】SN:{Barcode},扭矩规格:{TorqueMin}-{TorqueMax},当前扭矩:{Actual_Torque}");
                    return "NG";
                }
            }
        }
        public double Angle { get; set; }
        /// <summary>
        /// 角度结果
        /// </summary>
        public string Angle_Result
        {
            get
            {
                if (Angle >= AngleMin
                    && Angle <= AngleMax)
                    return "OK";
                else
                {
                    this.Remark = location + "号螺丝角度超出规格上下限";
                    LogHelper.Instance.Warr($"【位置:{location}】SN:{Barcode},角度规格:{AngleMin}-{AngleMax},当前角度:{Angle}");
                    return "NG";
                }
            }
        }
        [SugarColumn(IsIgnore = true)]
        public string m_Accumulative_Result
        {
            get
            {
                if (Result == "PASS"
                    || CCD_Result == "OK"
                    || Torque_Result == "OK"
                    || Angle_Result == "OK"
                   )
                {
                    return "OK";
                }
                else
                    return "NG";
            }
        }
        /// <summary>
        /// 最终结果
        /// </summary>
        public string Accumulative_Result { get; set; }
        private string location;
        public string Location
        {
            get { return location; }
            set
            {
                location = value;
                var screwInfo = PartNoHelper.Instance.CurrentPartNo.Modules.Where(p => p.Name == this.Name).FirstOrDefault();
                if (screwInfo != null)
                {
                    this.TorqueMax = double.Parse(screwInfo.Parame["MaxTorque"]);
                    this.TorqueMin = double.Parse(screwInfo.Parame["MinTorque"]);
                    this.AngleMax = double.Parse(screwInfo.Parame["MaxAngle"]);
                    this.AngleMin = double.Parse(screwInfo.Parame["MinAngle"]);
                }
            }
        }

        private double targetTorque = 0;
        /// <summary>
        /// 目标扭矩，如：7.5
        /// </summary>
        public double Target_Torque
        {
            get
            {
                return targetTorque;
            }
        }
        /// <summary>
        /// 实际扭矩
        /// </summary>
        public double Actual_Torque { get; set; }
        /// <summary>
        /// 电批数据中获取到的最大扭矩
        /// </summary>
        public double PeakTorqueMax { get; set; }

        /// <summary>
        /// 扭矩上限
        /// </summary>
        public double TorqueMax { get; set; }

        /// <summary>
        /// 扭矩下限
        /// </summary>
        public double TorqueMin { get; set; }
        /// <summary>
        /// 角度上限
        /// </summary>
        public double AngleMax { get; set; }

        /// <summary>
        /// 角度下限
        /// </summary>
        public double AngleMin { get; set; }
        public double ProductHole_X { get; set; }
        public double ProductHole_Y { get; set; }
        public double ScrewHole_X { get; set; }
        public double ScrewHole_Y { get; set; }
        public double Offset_X { get; set; }
        public double Offset_Y { get; set; }
        public double OffsetAngle { get; set; }
        public double Productoffset_X { get; set; }
        public double Productoffset_Y { get; set; }
        /// <summary>
        /// 吸料报警次数
        /// </summary>
        public int Vac_alarm { get; set; }
        /// <summary>
        /// 抛料次数
        /// </summary>
        public int Tossing_screw { get; set; }
        /// <summary>
        /// 标记（螺丝状态）
        /// </summary>
        [SugarColumn(IsNullable = true)]
        public string Remark { get; set; }

        /// <summary>
        /// 螺丝时间
        /// </summary>
        public DateTime CreateTime { get; set; } = DateTime.Now;

        /// <summary>
        /// 螺丝文件名称
        /// </summary>
        public string FileName { get; set; }

        /// <summary>
        /// 螺丝图片地址
        /// </summary>
        public string FilePath { get; set; }
        public TB_Screw Clone()
        {
            return this.MemberwiseClone() as TB_Screw;
        }
        [Navigate(NavigateType.OneToOne, nameof(Pid))]
        public TB_Product Product { get; set; }
    }
}
